package com.lm.common.core.exception;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 断言
 *
 * @author lm
 * @date 2019/5/23 14:57
 */
public class BizAssert {

    public BizAssert() {
    }

    public static boolean isTrue(Boolean obj, String code, String message) {
        if (!obj) {
            throw new BizException(code, message);
        } else {
            return true;
        }
    }

    public static boolean isTrue(Boolean obj, String message) {
        isTrue(obj, "500" , message);
        return true;
    }

    public static boolean notNull(Object obj, String code, String message) {
        isTrue(!Objects.isNull(obj), code, message);
        return true;
    }

    public static boolean notNull(Object obj) {
        isTrue(!Objects.isNull(obj), "500" , "obj is null");
        return true;
    }

    public static boolean notEmpty(String str, String code, String message) {
        isTrue(!StringUtils.isEmpty(str), code, message);
        return true;
    }

    public static boolean notEmpty(String[] strArray, String code, String message) {
        isTrue(!ArrayUtils.isEmpty(strArray), code, message);
        return true;
    }

    public static boolean notEmpty(Collection<?> list, String code, String message) {
        isTrue(list != null && list.size() > 0, code, message);
        return true;
    }

    public static boolean notEmpty(Map<?, ?> map, String code, String message) {
        isTrue(map != null && map.size() > 0, code, message);
        return true;
    }

    public static boolean empty(String str, String code, String message) {
        isTrue(StringUtils.isEmpty(str), code, message);
        return true;
    }

    public static boolean empty(String[] strArray, String code, String message) {
        isTrue(ArrayUtils.isEmpty(strArray), code, message);
        return true;
    }

    public static boolean empty(List<?> list, String code, String message) {
        isTrue(CollectionUtils.isEmpty(list), code, message);
        return true;
    }

    public static boolean empty(Map<?, ?> map, String code, String message) {
        isTrue(MapUtils.isEmpty(map), code, message);
        return true;
    }

    public static boolean equal(String strS, String strD, String code, String message) {
        isTrue(strS != null && strD != null && strS.equals(strD), code, message);
        return true;
    }

    public static boolean equal(Integer intS, Integer intD, String code, String message) {
        isTrue(intS != null && intD != null && intS.equals(intD), code, message);
        return true;
    }

    public static boolean notEmpty(String str) {
        isTrue(StringUtils.isNotEmpty(str), "500" , "obj is null or blank");
        return true;
    }

    public static boolean notEmpty(String[] strArray) {
        isTrue(!ArrayUtils.isEmpty(strArray), "500" , "obj is null or blank");
        return true;
    }

    public static boolean notEmpty(List<?> list) {
        isTrue(list != null && list.size() > 0, "500" , "obj is null or blank");
        return true;
    }

    public static boolean notEmpty(Map<?, ?> map) {
        isTrue(map != null && map.size() > 0, "500" , "obj is null or blank");
        return true;
    }

}